package org.jb.coinche.client.business.handler.impl;

import org.jb.coinche.client.business.message.PlayCardMessage;
import org.jb.coinche.client.business.model.Auction;
import org.jb.coinche.client.business.model.Card;
import org.jb.coinche.client.business.model.Player;
import org.jb.coinche.client.business.model.RoundTurn;
import org.jb.coinche.client.business.model.Card.Color;
import org.jb.coinche.client.gui.card.AuctionPanel;
import org.jb.coinche.client.gui.card.CardList;
import org.jb.coinche.client.gui.card.CardPanel;
import org.jb.coinche.client.gui.card.SetPanel;
import org.jb.coinche.client.gui.card.StatusPanel;
import org.jb.coinche.client.gui.card.CardList.CardListener;
import org.jb.coinche.client.guistatemachine.GUIStateHandler;
import org.jb.coinche.client.guistatemachine.MessageSender;
import org.jb.coinche.client.user.UserLogin;

import com.google.inject.Inject;

/**
 * Handler pour l'etat "tour en cours"
 * -> affichage des cartes et selection d'une carte si on est l'usager courant
 * @author jeremie
 *
 */
public class GUIRoundTurnHandler implements GUIStateHandler<RoundTurn>, CardListener {

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#disable()
	 */
	@Override
	public void disable() {
		cardList.setCardListener(null);
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#enable(org.jb.coinche.client.guistatemachine.MessageSender, org.jb.coinche.client.guistatemachine.GUIStateHandler)
	 */
	@Override
	public void enable() {		
		cardList.setCardListener(this);
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#update(org.jb.coinche.client.guistatemachine.State)
	 */
	@Override
	public void update(RoundTurn state) {
		
		//GameWindow gw = Coinche.gameWindow;
		
		/*
		 * Mise à jour du titre de la fenetre avec l'enchere courante
		 */
		Auction auction = state.getRound().getAuction();
		auctionPanel.setCurrentAuction(auction);
		auctionPanel.setEditable(false);
		auctionPanel.refresh();
		
		Player p = state.getGame().getPlayer( user );
		/*
		 * Mise à jour des cartes sur la table
		 */
		if (p == null)
			p = state.getGame().getNextPlayer( null );
		
		for( CardPanel cp : setPanel.getCardPanels() ) {
						
			if (state.getCurrentPlayer().getName().equals( p.getName() )) {
				cp.setWaiting( p.getName() );
			} else {
				Card card = state.getPlayedCard(p);
				if (card == null) {
					cp.setParameters( p.getName(), "" );
				} else {
					cp.setParameters( p.getName(), card );
				}
			}
			
			p = state.getRound().getGame().getNextPlayer( p );
		}
		
		/*
		 * Mis à jour de ses propres cartes
		 */
		p = state.getRound().getGame().getPlayer( user );
		if (p != null)
			cardList.setCardList( p.getCards(), state.getPlayableCards(p) );
		
		/*
		 * Si on est le joueur courant, alors on active l'interaction
		 */
		Color askedColor = state.getAskedColor();
		
		if (state.getCurrentPlayer().getName().equals( user )) {
			cardList.setClickable( true );
			
			if (askedColor == null)
				statusPanel.setStatus("<b>A vous de jouer</b>");
			else
				statusPanel.setStatus("<b>A vous de jouer, " + askedColor.toText() + " demandé</b>");
		} else {
			cardList.setClickable(false);			
			statusPanel.setStatus( "A " + state.getCurrentPlayer().getName() + " de jouer" );
		}
		
	}
	
	/**
	 * 
	 */
	@Override
	public void cardSelected(Card card) {
		PlayCardMessage pcMsg = new PlayCardMessage();
		pcMsg.setCard(card);		
		sender.send(pcMsg);	
	}
	
	

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#getStateClass()
	 */
	@Override
	public Class<RoundTurn> getStateClass() {
		return RoundTurn.class;
	}

	
	@Inject
	private MessageSender sender;

	@Inject
	private CardList cardList;
	
	@Inject
	private SetPanel setPanel;
	
	@Inject
	private AuctionPanel auctionPanel;
	
	@Inject
	private StatusPanel statusPanel;
	
	@Inject @UserLogin
	private String user;
}
